package com.imaker.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.imaker.domain.ModulesInfo;
import com.imaker.service.ModulesService;
import com.imaker.utils.StringHelper;
import com.imaker.utils.UploadHelper;
import com.imaker.utils.Validate;

 /**
 * <p>程序名称：      AjaxAction.java</p>
 * <p>程序说明：       处理ajax 进行的相关操作</p>
 * <p>版权信息：       Copyright mayonghua</p>
 * <p>时间：          Nov 5, 2010 9:36:50 AM</p>	
 * @author：          mayonghua
 * @version：         Ver 0.1
 */
@Controller
@Scope("prototype")
public class AjaxAction extends BaseAction {

	private String type;
	private String message;//上传提示信息
	private String fileName;//上传后的文件名
	private String filePath;//上传文件的完整路径
	private String returnContent;//通用的返回内容字符串
	@Autowired
	private ModulesService modulesService;
	
	/**
	 * 用过ajax方式进行文件的上传
	 * @return
	 */
	public String ajaxUpload(){
		if(type==null||type==""){
			type= "logo";
		}
		Validate.collectionNotNull(upload);
		List<String> nameList = UploadHelper.saveImg(upload, uploadFileName, uploadContentType, type);
		message = "finish";
		fileName = nameList.get(0);
		filePath = getPath()+"/images/"+type+"/"+fileName;
		return SUCCESS;
	}
	
	/**
	 * 通过ajax方式浏览服务器指定目录下面的内容
	 * @return
	 */
	public String ajaxViewServer(){
		if(type==null||type==""){
			type= "logo";
		}
		List<String> fileNameList =  UploadHelper.viewServer(type);
		String path = getRequest().getContextPath();
//		将文件名组织成字符串进行返回
		StringBuffer sb = new StringBuffer();
		for(String fileName : fileNameList){
			sb.append(path+"/upload/images/"+type+"/"+fileName);
			sb.append("$@_@$");
		}
		fileName = sb.toString();
		return SUCCESS;
	}
	
	/**
	 * ajax方式查寻模块
	 * @return
	 */
	public String ajaxModules() throws Exception{
		//查询所有的模块列表
		List<ModulesInfo> modulesList = modulesService.findAll();
		StringBuffer sb = new StringBuffer();
		//迭代所有模块，并将其组装成字符串
		for(int i=0;i<modulesList.size();i++){
			ModulesInfo moi = modulesList.get(i);
			if(i%2==0){
				if(i!=modulesList.size()-1)
				sb.append("<tr  style=\"cursor:hand;\"><td title=\""+StringHelper.filterNull(moi.getDescr())+"\"><input type=\"checkbox\"   value=\""+moi.getId()+"@_@"+moi.getModulesName()+"\">"+moi.getModulesName()+"</td>");
				else
				sb.append("<tr  style=\"cursor:hand;\"><td title=\""+StringHelper.filterNull(moi.getDescr())+"\"><input type=\"checkbox\"  value=\""+moi.getId()+"@_@"+moi.getModulesName()+"\">"+moi.getModulesName()+"</td></tr>");
			}else {
				sb.append("<td title=\""+StringHelper.filterNull(moi.getDescr())+"\"><input type=\"checkbox\"  value=\""+moi.getId()+"@_@"+moi.getModulesName()+"\">"+moi.getModulesName()+"</td></tr>");
			}
		}
		returnContent = sb.toString();
		return SUCCESS;
	}
	
	
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public String getFilePath() {
		return filePath;
	}
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String getReturnContent() {
		return returnContent;
	}

	public void setReturnContent(String returnContent) {
		this.returnContent = returnContent;
	}

	public ModulesService getModulesService() {
		return modulesService;
	}

	public void setModulesService(ModulesService modulesService) {
		this.modulesService = modulesService;
	}
}
